/**
 * @Project: Runer
 * @Title: Activitylogin.java
 * @Package com.runlooker.runer.registerAndLogin
 * @author town
 * @date 2014-3-29 下午3:23:53
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.runlooker.runer.ui.login;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.runlooker.runer.R;
import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.service.XmppService;
import com.runlooker.runer.ui.base.BaseActivity;
import com.runlooker.runer.ui.footPrintView.FootprintViewActivity;
import com.runlooker.runer.ui.home.HomeActivty;
import com.runlooker.runer.ui.rigister.RegisterActivity;
import com.runlooker.runer.util.L;
import com.runlooker.runer.util.NetworkUtil;
import com.runlooker.runer.util.PreferenceUtils;
import com.runlooker.runer.util.T;

/**
 * @ClassName Activitylogin
 * @Description 模块描述
 * @author town
 * @Date 2014-3-29
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
	private static final String tag = LoginActivity.class.getSimpleName();
	public static final int LOGIN_SUCCESS = 1; // 登录成功
	public static final int LOGIN_FAILED = 2;// 登录失败
	public static final String LOGIN_ACTION = "com.runlooker.runer.login";
	private TextView mAcountTV;
	private TextView mPasswordTV;
	private Button mLoginBT;
	private Button mRegistBT;
	private Button mPasserby;
	private XmppService mXmppService;
	private String mPassword;
	private String mAccount;
	private Thread mLoginThread;

	ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mXmppService = ((XmppService.XMPPBinder) service).getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mXmppService = null;
		}
	};

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			hideCustomProgressDialog();
			switch (msg.what) {
			case LOGIN_SUCCESS:
				PreferenceUtils.setPrefString(LoginActivity.this,
						MyApplication.ACCOUNT_USERNAME_KEY, mAccount);
				PreferenceUtils.setPrefString(LoginActivity.this,
						MyApplication.ACCOUNT_PASSWORD_KEY, mPassword);
				Intent intent = new Intent(LoginActivity.this,
						HomeActivty.class);
				boolean flag = MyApplication
						.isAccountConfigured(LoginActivity.this);
				intent.setAction(LoginActivity.LOGIN_ACTION);
				startActivity(intent);
				finish();
				break;
			case LOGIN_FAILED:
				L.i(tag, "DISCONNECTED");
				T.showLong(LoginActivity.this, "登录失败 ！");
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = new Intent(LoginActivity.this, XmppService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initialize();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(mConnection);
	}

	/**
	 * 初始化该界面所有要使用的控件
	 * 
	 * @Title: initWidget
	 * @Description: 方法描述
	 */
	private void initialize() {
		mAcountTV = (TextView) findViewById(R.id.et_login_useracount);
		mPasswordTV = (TextView) findViewById(R.id.et_login_passwd);
		mLoginBT = (Button) findViewById(R.id.btn_login_login);
		mRegistBT = (Button) findViewById(R.id.btn_login_regist);
		mPasserby = (Button) findViewById(R.id.btn_login_passerby);

		mAcountTV.setText(PreferenceUtils.getPrefString(this,
				MyApplication.ACCOUNT_USERNAME_KEY, ""));
		mPasswordTV.setText("");

		mPasserby.setOnClickListener(this);
		mLoginBT.setOnClickListener(this);
		mRegistBT.setOnClickListener(this);
	}

	public void back(View v) {
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login_login:

			if (!(NetworkUtil.getNetworkState(this) == NetworkUtil.NETWORN_NONE)) {
				mPassword = mPasswordTV.getText().toString().trim();
				mAccount = mAcountTV.getText().toString().trim();
				if (mPassword.equals("") || mAccount.equals("")) {
					T.showShort(this, "密码和帐号不能为空");
				} else {
					if (mXmppService != null) {
						if (mLoginThread == null) {
							super.showCustomProgrssDialog("登录中");
							mLoginThread = new Thread(new Runnable() {
								@Override
								public void run() {
									int flag = 0;
									try {
										mXmppService.loginService(mAccount,
												mPassword);
										flag = LOGIN_SUCCESS;
									} catch (Exception e) {
										e.printStackTrace();
										flag = LOGIN_FAILED;
									}

									L.i(tag, "登录线程已开启");
									handler.sendEmptyMessage(flag);
									if (mLoginThread != null)
										synchronized (mLoginThread) {
											mLoginThread = null;
										}
								}
							});
							mLoginThread.start();
						}
					}
				}
			} else {
				T.showShort(this, "无法连接网络");
				handler.sendEmptyMessage(0);
			}
			break;
		case R.id.btn_login_regist:
			Intent intent1 = new Intent(LoginActivity.this,
					RegisterActivity.class);
			startActivity(intent1);
			break;
		case R.id.btn_login_passerby:
			Intent intent2 = new Intent(LoginActivity.this,
					FootprintViewActivity.class);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
}
